/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
 var reverseEvenLengthGroups = function(head) {
  let cur = head, cnt = 1
  let pre = head
  while(cur){
    let i = cnt
    let arr = []
    let before = pre
    while(i-- && cur){
      arr.push(cur)
      pre = cur
      cur = cur.next
    }
    if(arr.length && arr.length % 2 === 0){
      pre = before
      let next = arr[arr.length-1].next
      while(arr.length){
        pre.next = arr.pop()
        pre = pre.next
      }
      pre.next = next
    }
    cnt++
  }
  return head
};